package com.example.bigevent.service.impl;

import com.example.bigevent.mapper.ArticleMapper;
import com.example.bigevent.pojo.Article;
import com.example.bigevent.pojo.PageBean;
import com.example.bigevent.service.ArticleService;
import com.example.bigevent.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 新增文章
     */
    @Override
    public void add(Article article) {
        //补充用户id
        Map<String,Object> map= ThreadLocalUtil.get();
        Integer userId= (Integer) map.get("id");
        article.setCreateUser(userId);
        //新增
        articleMapper.insert(article);
    }

    /**
     * 更新文章
     */
    @Override
    public void update(Article article) {
        articleMapper.update(article);
    }

    /**
     * 根据id查询文章详细信息
     */
    @Override
    public Article findById(Integer id) {
       return articleMapper.findById(id);
    }

    /**
     * 根据id删除文章
     */
    @Override
    public void deleteById(Integer id) {
        articleMapper.deleteById(id);
    }

    /**
     * 查询文章列表
     */
    @Override
    public PageBean<Article> list(Integer pageNum, Integer pageSize, Article article) {
        //填充用户id
        Map<String,Object> map=ThreadLocalUtil.get();
        article.setCreateUser((Integer) map.get("id"));
        PageBean<Article> pageBean = sysList(pageNum, pageSize, article);
        return pageBean;
    }

    /**
     * 查询后台文章列表
     */
    @Override
    public PageBean<Article> sysList(Integer pageNum, Integer pageSize, Article article) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        List<Article> list= articleMapper.selectArticleList(article);
        //强转子类，调用子类的功能
        Page<Article> page= (Page<Article>) list;
        PageBean<Article> pageBean=new PageBean<>(page.getTotal(),page.getResult());
        return pageBean;
    }
}
